# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2024/10/26 13:58
import requests
import json
from datetime import datetime


class Ding(object):

    def ding_hami(self,content_text):
        # 钉钉机器人的Webhook URL
        url = "https://oapi.dingtalk.com/robot/send?access_token=a7559e1723520fd260c674e404e34b03cb328c73190e8e2e3ea44275d6c2fd77"
        # 消息内容
        message = {
            "msgtype": "text",
            "text": {
                "content": content_text
            }

        }
        # 发送请求
        response = requests.post(url, data=json.dumps(message), headers={'Content-Type': 'application/json'})

        # 打印响应
        print(response.text)


    # 拼装测试结果
    def report_message(self, report=None, report_list='http://47.117.183.237:8000/#', phone=''):
        if report is None:
            report = {}  # 避免可变默认值问题

        # 将字典转换为多行文本（每行一个键值对）
        report_lines = []
        for key, value in report.items():
            report_lines.append(f"{key}: {value}")
        report_text = '\n'.join(report_lines) if report_lines else "暂无统计信息"

        message = {
            "msgtype": "markdown",
            "markdown": {
                "title": "冒烟测试",
                "text": (
                    f"##冒烟测试：\n"
                    f"{report_text}\n\n"  # 换行展示字典内容
                    f"报告地址: {report_list}\n"
                    f"@{phone}"
                )
            },
            "at": {
                "atMobiles": [phone],
                "isAtAll": False
            }
        }
        return message


    def send_to_dingtalk(self,webhook_url, message):
        headers = {'Content-Type': 'application/json'}
        try:
            response = requests.post(webhook_url, json=message, headers=headers)
            return response.status_code == 200
        except Exception as e:
            print(f"消息发送失败：{str(e)}")
            return False


if __name__ == "__main__":
    content_text = """
    车辆
    换行
    """
    print(content_text)
    #Ding().ding_hami("测试")

